package domein;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SimpleMaze extends ExtendableMap {

	private static int dimension = 9;
	private static double straightPathCost = 1.0, diagonalPathCost = Math
			.sqrt(2);
	private static List<Integer> obstacles;

	public SimpleMaze(ExtendableMap map) {
		initialize(map);
	}

	public static void initialize(ExtendableMap map) {
		map.clear();
		addObstacles();
		createPaths(map);
	}

	private static void createPaths(ExtendableMap map) {
		int x = 0;
		int y = 0;
		Set<Integer> pathpoints = new HashSet<Integer>();

		for (int row = 0; row < dimension; row++) {
			for (int column = 1; column < dimension; column++) {
				int pathpoint = column + row * dimension;

				if (!obstacles.contains(pathpoint)
						&& !obstacles.contains(pathpoint + 1))
					map.addBidirectionalLink("" + pathpoint, ""
							+ (pathpoint + 1), straightPathCost);

				pathpoints.add(pathpoint);
				pathpoints.add(pathpoint + 1);

				if (pathpoint % dimension != 0
						&& pathpoint <= (dimension * (dimension - 1))) {
					if (!obstacles.contains(pathpoint)
							&& !obstacles.contains(pathpoint + dimension + 1)) {
						map.addBidirectionalLink("" + pathpoint, ""
								+ (pathpoint + dimension + 1), diagonalPathCost);
					}
				}
			}
		}

		List<Integer> pathpointList = new ArrayList<Integer>(pathpoints);
		Collections.sort(pathpointList);
		int counter = 0;

		for (int pathpoint : pathpointList) {
			if (pathpoint % dimension != 1
					&& pathpoint <= (dimension * (dimension - 1)))
				if (!obstacles.contains(pathpoint)
						&& !obstacles.contains(pathpoint + dimension - 1))
					map.addBidirectionalLink("" + pathpoint, ""
							+ (pathpoint + dimension - 1), diagonalPathCost);

			if (!obstacles.contains(pathpoint)
					&& !obstacles.contains(pathpoint - dimension))
				if (pathpoint > dimension)
					map.addBidirectionalLink("" + pathpoint, ""
							+ (pathpoint - dimension), straightPathCost);

			map.setDistAndDirToRefLocation("" + pathpoint, x, y);
			x += 50;
			counter++;

			if (counter == dimension) {
				x = 0;
				y += 50;
				counter = 0;
			}
		}
	}

	public static int getGoalPosition() {
		return 5;
	}

	public static int getStartPosition() {
		return 73;
	}

	private static void addObstacles() {
		obstacles = new ArrayList<Integer>();

		obstacles.add(10);
		obstacles.add(11);
		obstacles.add(12);
		obstacles.add(13);
		obstacles.add(14);
		obstacles.add(15);
		obstacles.add(16);
		obstacles.add(17);
		obstacles.add(26);
		obstacles.add(35);
		obstacles.add(44);
		obstacles.add(53);
		obstacles.add(62);
		obstacles.add(71);
		obstacles.add(79);
	}
}
